package service

import (
	"path/filepath"
	"strings"
)

// ContentUtils 内容工具类
type ContentUtils struct{}

// NewContentUtils 创建内容工具类
func NewContentUtils() *ContentUtils {
	return &ContentUtils{}
}

// GenerateSummary 生成文本摘要
func (u *ContentUtils) GenerateSummary(text string, summaryLength int) string {
	// 如果文本很短，直接返回
	if len(text) <= summaryLength {
		return text
	}

	// 简单的摘要生成策略：取前N个字符
	summary := text[:summaryLength]

	// 确保摘要以完整的句子结束
	lastSentenceEnd := strings.LastIndexAny(summary, "。？！.?!")
	if lastSentenceEnd > 0 {
		summary = summary[:lastSentenceEnd+1]
	}

	return summary
}

// GetContentType 获取内容类型
func (u *ContentUtils) GetContentType(filePath string) string {
	ext := strings.ToLower(filepath.Ext(filePath))
	switch ext {
	case ".md":
		return "text/markdown"
	case ".txt":
		return "text/plain"
	case ".c":
		return "text/x-c"
	case ".cpp", ".cxx", ".cc":
		return "text/x-c++src"
	case ".h", ".hpp":
		return "text/x-chdr"
	case ".js":
		return "application/javascript"
	case ".ts":
		return "application/typescript"
	case ".go":
		return "text/x-go"
	case ".py":
		return "text/x-python"
	case ".java":
		return "text/x-java-source"
	default:
		return "application/octet-stream"
	}
}

// MinInt 返回两个整数中的较小值
func (u *ContentUtils) MinInt(a, b int) int {
	if a < b {
		return a
	}
	return b
}